package com.rw.controller;

import com.rw.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;

@WebServlet(name = "CheckAccountServlet", value = "/check")
public class CheckAccountServlet extends HttpServlet {

    private AccountService accountService;

    @Override
    public void init() throws ServletException {
//        super.init();
//        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
        ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        this.accountService = (AccountService) ac.getBean("accountService");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        //根据调用者调用的方法来进行确定调用哪一个方法
        String methodParam = request.getParameter("method");
        if ("getCheckAccount".equals(methodParam)){
            this.getCheckAccount(request,response);
        }else if("getCheckMoney".equals(methodParam)){
            this.getCheckMoney(request,response);
        }else if ("getCheckInAccount".equals(methodParam)){
            this.getCheckInAccount(request,response);
        }else {
            System.out.println("错误的方法信息");
        }


    }

    /**
     * 校验收款人的信息
     * @param request
     * @param response
     */
    private void getCheckInAccount(HttpServletRequest request, HttpServletResponse response) {
        Integer inUid = Integer.parseInt(request.getParameter("inAid"));
        //防止中文乱码
        String inUname = request.getParameter("inUname");
        System.out.println(inUname);
        try {
            //校验s收款人账户信息
            boolean flag = this.accountService.getCheckInAccount(inUid,inUname);
//            if (flag){
//                response.getWriter().println("true");
//            }else {
//                response.getWriter().println("false");
//            }
            this.printData(response,flag);
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 校验账户的方法
     * @param request
     * @param response
     */
    private void getCheckAccount(HttpServletRequest request, HttpServletResponse response){
        Integer aid = Integer.parseInt(request.getParameter("aid"));
        String apwd = request.getParameter("apwd");
        try {
            //校验账户信息
            boolean flag = this.accountService.getCheckAccount(aid, apwd);
//            if (flag){
//                response.getWriter().println("true");
//            }else {
//                response.getWriter().println("false");
//            }
            this.printData(response,flag);
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }

    private void getCheckMoney(HttpServletRequest request, HttpServletResponse response){
        //进行数据查询
        Integer aid = Integer.parseInt(request.getParameter("aid"));
        Double money = Double.parseDouble(request.getParameter("money"));
        try {
            //校验的是金额
            boolean flag = this.accountService.getCheckMoney(aid, money);
            this.printData(response,flag);
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }

    public void printData(HttpServletResponse response, boolean flag) throws IOException {
        if (flag){
            //如果有一天需要修改返回的结果
            response.getWriter().println("true");
        }else {
            response.getWriter().println("false");
        }
    }


}
